﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.Model;
using FantastPlatform.Framework.Service;
using FantastPlatform.Community.IService;
using NHibernate;
using NHibernate.Criterion;

namespace FantastPlatform.Community.Service
{
    public class HouseDistrictService : DataService<IHouseDistrict, HouseDistrict>, IHouseDistrictService
    {

        public IList<IHouseDistrict> SearchByCondition(string name, string streetName, int policeManId)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(HouseDistrict));
            criteria.CreateAlias("Street", "s").
                Add(Expression.Or(Expression.Like("s.Name", "%" + streetName + "%"),
                Expression.Like("Address", "%" + streetName + "%")))
                .Add(Expression.Like("Name", "%" + name + "%"))
                .CreateAlias("PoliceMan", "p").Add(Expression.Eq("p.Id", policeManId))
                .Add(Expression.Eq("Enable", true));
            return criteria.List<IHouseDistrict>();
        }


        public IHouseDistrict GetHouseDistrictByAddress(string address,string name)
        {
            string hql = "select a.* from dbo.HouseDistrict as a,dbo.Base_QYStreet as b where a.StreetId = b.id and b.Name + a.[address]  = '" + address + "' and a.name = '" + name + "'";

            IQuery query = Session.CreateSQLQuery(hql, "a", typeof(HouseDistrict));
            IList<IHouseDistrict> list = query.List<IHouseDistrict>();
            if (list.Count > 0)
                return list[0];
            else
                return null;
        }
        public override void Update(IHouseDistrict entity)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tran) =>
            {

                entity.ModifyDate = DateTime.Now;
                entity.Enable = true;
                Session.Merge(entity);
            }, transaction);
        }
    }
}
